﻿using DeHeng_Mes.Model.Table;
using DeHeng_Mes.Utils;
using W03_UI.Table;
using W03_UI.Utils;
using zjxj_repair.Bean;
using zjxj_repair.Table;
using zjxj_repair.Utils;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace zjxj_repair.Dao
{
    public class EnableDao
    {
        /// <summary>
        /// 当前工具是否出于使能状态
        /// </summary>
        /// <param name="toolName"></param>
        /// <returns></returns>
        public string ToolIsEnable(string toolName)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<RepairToolStatus>().Where(x => x.ToolName.Equals(toolName)).Select(x => x.Status).First();
            }
        }

        /// <summary>
        /// 更新当前工具使用状态
        /// </summary>
        /// <param name="toolName"></param>
        public bool ToolEnable(string toolName, string status)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Updateable<RepairToolStatus>()
                         .SetColumns(x => x.Status == status)
                         .Where(x => x.ToolName == toolName)
                         .ExecuteCommand() > 0;
            }
        }

        /// <summary>
        /// 备份返修信息
        /// </summary>
        /// <param name="vinCode"></param>
        /// <param name="backString"></param>
        /// <param name="dateTime"></param>
        public void SaveBackString(string vinCode, string backString, DateTime dateTime, string status, string model)
        {
            using (var db = SqlSugarConfig.Db)
            {
                 db.Insertable(new RepairBack()
                {
                    BackString = string.IsNullOrEmpty(backString) ? "正常流程" : backString,
                    CreateTime = dateTime,
                    VinCode = vinCode,
                    Status = status,
                    Model = model
                }).ExecuteCommand();
            }
        }

        /// <summary>
        /// 查询CCD的NG点位
        /// </summary>
        /// <param name="vinCode"></param>
        /// <returns></returns>
        public List<string> QueryCCDData(string vinCode)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<TraceCCD>().Where(x => x.TraceId.Equals(vinCode) && !x.Result.Equals("1")).Select(x => x.PointName).ToList();
            }
        }

        /// <summary>
        /// 查询拧紧数据
        /// </summary>
        /// <param name="vinCode"></param>
        /// <returns></returns>
        public List<string> QueryTightenData(string vinCode)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<TraceTighten>().Where(x => x.TraceId.Equals(vinCode) && !x.Result.Equals("1")).Select(x => x.RangeNo).ToList();
            }
        }

        /// <summary>
        /// 查询铆接数据
        /// </summary>
        /// <param name="vinCode"></param>
        /// <returns></returns>
        public List<string> QueryRiveteData(string vinCode)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<TraceRivete>().Where(x => x.TraceId.Equals(vinCode) && !x.Result.Equals("1")).Select(x => x.PointNo).ToList();
            }
        }

        /// <summary>
        /// 查询FDS数据
        /// </summary>
        /// <param name="vinCode"></param>
        /// <returns></returns>
        public List<string> QueryFDSData(string vinCode)
        {
            using (var db = MySqlSugarConfig.Db)
            {
                return db.Queryable<TraceFDS>()
                         .Where(x => x.ScrewID == vinCode && !x.OK_NOK_Code.Equals("1"))
                         .Select(x => x.SystemId)
                         .ToList();
            }
        }

        /// <summary>
        /// 更新工具配置数据
        /// </summary>
        /// <param name="repairs"></param>
        public void UpdateToolConfig(RepairToolConfig repairs)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<RepairToolConfig>()
                  .SetColumns(x => new RepairToolConfig()
                  {
                      ToolIp = repairs.ToolIp,
                      ToolPort = repairs.ToolPort
                  })
                  .Where(x => x.ToolName == repairs.ToolName)
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 查询工具配置数据
        /// </summary>
        /// <returns></returns>
        public List<RepairToolConfig> QueryToolConfig()
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<RepairToolConfig>().ToList();
            }
        }

        /// <summary>
        /// 存储返修记录
        /// </summary>
        /// <param name="repairData"></param>
        public void SaveRepairData(RepairData repairData)
        {
            using (var db = SqlSugarConfig.Db)
            {
                StaticBean.RepairData.Add(repairData);

                db.Insertable(repairData).ExecuteCommand();
            }
        }

        /// <summary>
        /// 存储拧紧数据
        /// </summary>
        /// <param name="traceTighten"></param>
        public void SaveTightenData(TraceTighten traceTighten)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Insertable(traceTighten).AS($"trace_tighten{HashUtils.GetHashAsInt(traceTighten.TraceId, 10)}").ExecuteCommand();

                Tools.ProduceLogText($"[{traceTighten.Station}]存储了一条拧紧数据，扭矩[{traceTighten.Torque}]，角度[{traceTighten.Angle}]，二维码[{traceTighten.TraceId}]，结果[{traceTighten.Result}]");
            }
        }

        /// <summary>
        /// 查询返修数据
        /// </summary>
        /// <param name="vinCode"></param>
        /// <returns></returns>
        public List<RepairData> QueryRepairData(string vinCode)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<RepairData>().Where(x => x.VinCode.Equals(vinCode)).ToList();
            }
        }

        /// <summary>
        /// 返回未使能的工具
        /// </summary>
        /// <param name="toolNameNow"></param>
        /// <returns></returns>
        public List<RepairToolStatus> ToolAllIsEnable(string toolNameNow)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<RepairToolStatus>()
                         .Where(x => x.ToolName.Contains(toolNameNow))
                         .ToList();
            }
        }
    }
}
